package com.nondo.dean.saprfc.service;

import com.nondo.dean.saprfc.common.SapUtil;
import com.nondo.dean.saprfc.common.Util;
import com.nondo.dean.saprfc.domain.ProductInventory;
import com.nondo.dean.saprfc.domain.ProductMaterialBean;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoTable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xiaofan
 * @Description: 获取sap物料数据
 * @throws
 * @date 2018-03-299:43
 */
@Service
public class CrmProductMaterialService {

    /**
     * 获取计量单位
     * @param SapClient
     * @return
     * @throws Exception
     */
    public Map<String, String> getMeasurement(String SapClient) {
        Map<String, String> hashMap=new HashMap<String, String>();
        ArrayList<String> tmp_list=new ArrayList<String>();
        JCoFunction function = SapUtil.getFunction(SapClient,"Z_ND_OA_MATNR_A");
        SapUtil.executeFunction(SapClient,function);
        JCoTable V_TABLE=function.getTableParameterList().getTable("EX_T006A");
        for (int i = 0; i < V_TABLE.getNumRows(); i++) {
            //编号
            String v_MSEHI=V_TABLE.getString("MSEHI");
            //名称
            String v_MSEHL=V_TABLE.getString("MSEHL");
            if("".equals(v_MSEHL)){
                tmp_list.add("'"+v_MSEHL+"'");
                tmp_list.add("char(13)");
                tmp_list.add("char(10)");
            }
            hashMap.put(v_MSEHI, v_MSEHL);
            V_TABLE.nextRow();
        }
        if(tmp_list.isEmpty()==true){
            tmp_list.add("''");
        }
        String tmp_str="CONCAT("+tmp_list.toString()+")";
        tmp_str=tmp_str.replace("[", "").replace("]", "");
        String v_sql="update crm_sys_picklist set picklist_items="+tmp_str+" where picklist_name='PICKLIST_35'";

        return hashMap;
    }

    /**
     *获取sap的产品物料信息
     * @param SapClient
     * @return
     */
    public ArrayList<ProductMaterialBean> getSapMaterial(String SapClient)  {
        ArrayList<ProductMaterialBean> materialBeanList=new ArrayList<ProductMaterialBean>();
        //价格级别
        Map<Integer, Integer> priceTypeMap= new HashMap<Integer, Integer>();
        priceTypeMap.put(01, 0);
        priceTypeMap.put(02, 1);
        priceTypeMap.put(03, 2);
        //返回计量单位
        Map<String, String> priceUnitMap=this.getMeasurement(SapClient);

        JCoFunction function = SapUtil.getFunction(SapClient,"Z_ND_OA_MATNR_A");
        SapUtil.executeFunction(SapClient,function);
        JCoTable v_table=function.getTableParameterList().getTable("EX_MASTER");
        for (int i = 0; i < v_table.getNumRows(); i++) {
            //物料号
            String v_code=v_table.getString("MATNR");
            //物料描述（短文本）
            String v_name=v_table.getString("MAKTX");
            //物料组
            String v_type=v_table.getString("MATKL");
            //基本计量单位
            String v_unit=v_table.getString("MEINS");
            //物料定价组
            Integer v_priceGrade=Integer.parseInt(v_table.getString("KONDM"));
            //该物料的科目设置组
            String v_materialFlag=v_table.getString("KTGRM");
            //跨工厂物料状态
            String v_MSTAE=v_table.getString("MSTAE");
            //跨分销链物料状态
            String v_MSTAV=v_table.getString("MSTAV");
            //总代采购价格
            String v_ZDCGJG=v_table.getString("ZDCGJG");
            //总代销售价格
            String v_ZDXSJG=v_table.getString("ZDXSJG");
            //货币码
            String v_WAERS=v_table.getString("WAERS");
            //一般标记
            String v_FLAG=v_table.getString("FLAG");
            //可下单标识
            String v_XFLAG=v_table.getString("XFLAG");
            //厂内生产时间
            String v_DZEIT=v_table.getString("DZEIT");
            //删除标识符
            String v_LVORM=v_table.getString("LVORM");
            //20渠道物料标识
            String channel20=v_table.getString("ZFLAG20");
            //40渠道物料标识
            String channel40=v_table.getString("ZFLAG40");
            //分销渠道
            String v_VTWEG=v_table.getString("VTWEG");
            //事业部代码
            String businessCode=v_table.getString("LABOR");

            //删除物料号前的0
            v_code= Util.delFirstZero(v_code);
            //软体标识
            String tmp_ISRT = "A".equals(v_FLAG) ? "软体" : "其它";
            //物资标识
            String tmp_KTGRM = "03".equals(v_materialFlag) ? "饰品物资" : "生产成品";
            //可下单标识
            String signable = "X".equals(v_XFLAG) ? "是" : "否";
            //20渠道物料标识
            channel20 = "X".equals(channel20) ? "是" : "否";
            //40渠道物料标识
            channel40 = "X".equals(channel40) ? "是" : "否";

            //删除标识
            String delFlag="0";
            if("03".equals(v_MSTAE)){
                delFlag="1";
            }else if("X".equals(v_LVORM)){
                delFlag="1";
            }
            Integer v_stauts = null;
            // 0已淘汰 1逐步淘汰
            if("1".equals(delFlag)){
                v_stauts=0; 
            }else if("DE".equals(v_MSTAE)){
                v_stauts=1;
            }
            //软床事业部
            if("NS1".equals(businessCode)){
                businessCode="NS01";
            }else{
                //成浪
                businessCode="ND00";
            }

            //价格级别
            v_priceGrade=priceTypeMap.get(v_priceGrade);
            //计量单位
            v_unit=priceUnitMap.get(v_unit);

            ProductMaterialBean  productMaterialBean=new ProductMaterialBean(v_code,v_name,v_type, v_unit,
                                                         v_materialFlag,tmp_KTGRM, v_DZEIT, signable, v_stauts,
                                                         v_priceGrade,v_ZDCGJG, v_ZDCGJG,  delFlag, channel20,
                                                         channel40, businessCode);
            materialBeanList.add(productMaterialBean);

            v_table.nextRow();
        }
        return materialBeanList;
    }

    /**
     * 获取sap的产品库存信息
     * @return
     */
    public ArrayList<ProductInventory> getSapProductInventory(String sapCilent){
        ArrayList<ProductInventory> productInventoryList = new  ArrayList<ProductInventory>();
        JCoFunction function = SapUtil.getFunction(sapCilent,"Z_SD_KC");
        SapUtil.executeFunction(sapCilent,function);
        //参考库存
        JCoTable V_TABLE=function.getTableParameterList().getTable("EX_KC");
        for (int i = 0; i < V_TABLE.getNumRows(); i++) {
            //物料号
            String v_matnr=V_TABLE.getString("MATNR");
            //库存数量
            String v_labst=V_TABLE.getString("LABST");

            ProductInventory bean=new ProductInventory(v_matnr, v_labst,"EX_KC");
            productInventoryList.add(bean);
            V_TABLE.nextRow();
        }
        //在制库存
        V_TABLE=function.getTableParameterList().getTable("EX_MD");
        for (int i = 0; i < V_TABLE.getNumRows(); i++) {
            //物料号
            String v_matnr=V_TABLE.getString("MATNR");
            //库存数量
            String v_labst=V_TABLE.getString("LABST");

            ProductInventory bean=new ProductInventory(v_matnr, v_labst,"EX_MD");
            productInventoryList.add(bean);
            V_TABLE.nextRow();
        }

        return productInventoryList;
    }
}
